package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long id) {
        //根据id查找子节点
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, id);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category -> {
                //查找当前节点是否有子节点
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLeveOneCategory() {
        //查询所有一级分类
        List<Category> allCategoryList = baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        //转换为vo
        return allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVo> tree() {
        //查询所有分类
        List<Category> allCategoryList = baseMapper.selectList(null);

        List<CategoryVo> categoryVoList = allCategoryList.stream().map(item -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());
        return buildTree(categoryVoList);
    }

    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //创建树形结构
        ArrayList<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否是根节点
            if (categoryVo.getParentId().longValue() == 0) {
                //查找当前节点的子节点，添加到树形结构中
                categoryVo = findAndAddChildren(categoryVo, categoryVoList);
                trees.add(categoryVo);
            }
        }
        return trees;
    }

    /**
     * 在categoryVoList中找到categoryVo的子节点，并组装到categoryVo中
     *
     * @param categoryVo
     * @param categoryVoList
     * @return 返回组装好的categoryVo节点
     */
    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            //判断是否是category的子节点
            if (node.getParentId().longValue() == categoryVo.getId().longValue()) {

                CategoryVo children = findAndAddChildren(node, categoryVoList);
                //组装到categoryVo中
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }
}
